package in.muton.edushare.poll.answers;

import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.poll.questions.CheckBoxQuestion;

import java.text.DecimalFormat;
import java.text.ParseException;

import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.JavascriptPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.dao.UserPollDAO;
import com.edushare.common.vo.PollQuestions;

public class RatingAnswer extends AnswerBase {

	public RatingAnswer(String id, IModel<PollQuestions> model,
			EduShareBasePage page) {
		super(id, model, page);
		add(JavascriptPackageResource.getHeaderContribution(
				CheckBoxQuestion.class, "questions.js"));

		Double rating = new UserPollDAO(getORMInterface())
				.getAverageRating(getQuestionModel().getObject());

		DecimalFormat df = new DecimalFormat("##.#");
		try {
			add(new Label("rating", ""
					+ df.parse(df.format(rating)).doubleValue()));
		} catch (ParseException e) {
		}
		add(new Label("responses", new PropertyModel<Integer>(this,
				"totalAnswers")));
		WebMarkupContainer wmc = new WebMarkupContainer("sliderReply");
		add(wmc);
		wmc.add(new SimpleAttributeModifier("rating", "" + rating.intValue()));
	}

	public Integer getTotalAnswers() {
		return getQuestionModel().getObject().getAnswers().size();
	}

	private static final long serialVersionUID = -7897656843861928324L;
}
